//https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    int removeDuplicates(vector<int>& nums)
    {
        for (auto it = nums.begin(); it < nums.end() - 1;)
        {
            int flag = 0;
            int num = 0;
            auto fi = it;

            while (fi != nums.end() && *it == *fi)
            {
                fi++;
                num++;
            }

            if (num > 2)
                flag = 1;

            if (flag)
            {
                it += 2;
                it = nums.erase(it, fi);
            }
            else
                it++;

        }
        return nums.size();
    }
};